/**
 * Created by koudaiwang on 2017/6/20.
 */

var fs = require('fs');
var path = require('path');
var XXXModel = require('../Dao/admindao');
var JsonModel = require('../config/jsonModel');
var MD5S = require('../tool/MD5S');
var CheckLogin = require('./checkLogin');

module.exports = function(app) {

    app.get('/test', CheckLogin.checkAdminLogin ,function (req, res) {
        var txt = req.query.txt;
        res.send(MD5S.md5s(txt));
    });
    // -------------- 路径隐藏下
    app.post('/addxxx', CheckLogin.checkAdminLogin ,function (req, res) {


        var model = new JsonModel.JsonModel();
        model.status = 0;
        var username = req.body.username;
        var passwords = req.body.passwords;
        var repasswords = req.body.repasswords;
        var level = req.body.level;
        // var avatar = req.files.avatar.path.split(path.sep).pop();
        try {
            if (!(username.length >= 1 && username.length < 10)) {
                throw new Error('用户名限制在1-10个字符!');
            }
            if (!passwords) {
                throw new  Error('密码不能为空!');
            }
            if (passwords !== repasswords) {
                throw new Error('两次输入的密码不一致!');
            }
            // if (!req.files.avatar.name) {
            //     throw new Error('缺少头像');
            // }
            if (!level) {
                throw new Error('选择用户权限等级!');
            }
        } catch (e) {
            model.content = e.message;
            model.msg = e.message;
            return res.send(model);
        }


        var xxx = {
            username: username,
            passwords: passwords,
            avatar: 'avatar',
            level: Number(level)
        };
        XXXModel.addAdmin(xxx)
            .then(function (result) {
                var ss = result.ops[0];
                model.content = ss;
                model.status = 1;
                model.msg = '添加成功!';
            res.send(model);
        }).catch(function (error) {
            // fs.unlink(req.files.avatar.path);
            model.content = {};
            if (error.message.match('E11000 duplicate key')) {
                model.msg = '用户名重复!';
            } else {
                model.msg = error.message;
            }
             res.send(model);
        });

    });

    app.post('/xxxLogin', function (req, res) {
        console.log(req);
        var username = req.body.username;
        var passwords = req.body.passwords;
        var deviceType = req.body.deviceType;
        var model = new JsonModel.JsonModel();
        model.status = 0;

        try {
            if (!username) {
                throw new Error('用户名不能为空!');
            }
            if (!passwords) {
                throw new Error('密码不能为空!');
            }
            if (!deviceType) {
                throw new Error('设备类型不能为空!');
            }
        } catch (e) {
            model.content = e.message;
            model.msg = e.message;
            return res.send(model);
        }

        XXXModel.getAdminByName(username).then(function (result) {
            console.log(result);
            var user = result[0];
            if (!user) {
                model.content = {};
                model.msg = '没有此用户!';
                return res.send(model);
            } else {
                if (username === user.username && passwords === user.passwords) {
                    var data = {};
                    var token = MD5S.md5token(user._id);
                    if (deviceType.toLowerCase() === 'web') {
                        data['webLogin'] = token;
                        data['webLoginTime'] = new Date().getTime().toString();
                        model.content = {username: user.username, sessionID: token, level: user.level};
                    } else if (deviceType.toLowerCase() === 'ios') {
                        data['iOSLogin'] = token;
                        data['iOSLoginTime'] = new Date().getTime().toString();
                        model.content = {username: user.username, sessionID: token, level: user.level};
                    } else if (deviceType.toLowerCase() === 'android') {
                        data['androidLogin'] = token;
                        data['androidLoginTime'] = new Date().getTime().toString();
                        model.content = {username: user.username, sessionID: token, level: user.level};
                    } else {
                        model.content = {};
                        model.msg = '设备类型不正确!';
                        return res.send(model);
                    }
                    XXXModel.updateLoginStatus(user._id, data).then(function () {
                        model.status = 1;
                        model.msg = '登录成功!';
                        return res.send(model);
                    }).catch(function (error) {
                        model.content = {};
                        model.msg = error.message;
                        return res.send(model);
                    }); } else  {
                    model.content = {};
                    model.status = 0;
                    model.msg = '密码不正确!';
                    return res.send(model);
                }
            }

        });
    });

    app.post('/logout', function (req, res) {

    });

    app.get('/allxxx', CheckLogin.checkAdminLogin ,function (req, res) {
        XXXModel.getAllAdmin().then(function (result) {
            return res.send(result);
        })
    });

}